
use "${data}Mobarak_replication_data.dta", replace 	
	
	g t1_unmarried= t1*  unmarried13

	g spouse_inc_zero= spouse_incLmonth	 
	replace spouse_inc_zero=0 if married==0
	label var spouse_inc_zero "spouse income (zeros)"


global option varlabels tex plain fragment bdec() se starloc(1) starlevels(10 5 1) nocenter ///
summstat(r2\N) summtitle("R^2" \"Observations") summdec(3 0) blankrows
			
global option_B varlabels tex plain fragment bdec(0) se starloc(1) starlevels(10 5 1) nocenter ///
summstat(r2\N) summtitle("R^2" \"Observations") summdec(3 0) blankrows 
		
	
outreg,clear(dep_final)
tempvar samplee
g `samplee' =  t2==0 
		
foreach outcome in  married	{
	local labout : variable label `outcome'					
	 su `outcome' [aw=wt] if `samplee'==1 & t1==0
		local av: di %4.3f r(mean)	 	
		
	 ivreg2  `outcome' (migrated = t1 )  $x  i.upazila [pw=wt] if `samplee' ==1, cluster(UNION)				
	qui outreg, merge(dep_final) nod keep(migrated ) ///
	ctitle("dep.var:", "`labout'" \ "","" )  addrows( "","" \  "Average outcome in control group", `av'  )  ${option} 		
	qui ivreg2 `outcome' (migrated migUnmarried = t1 c.t1#c.unmarried13) unmarried13 $x i.upazila [pw=wt]  if `samplee' ==1, cluster(UNION)
	qui outreg, merge(dep_final) nod keep(migrated migUnmarried unmarried)   ///
	ctitle("dep.var:", "`labout'"  \ "","" )   ///
					 addrows( "","" \  "Average outcome in control group", `av' )  ${option} 			
}
foreach outcome in    spouse_ed8	{
	local labout : variable label `outcome'					
	 su `outcome' [aw=wt] if `samplee'==1 & t1==0
		local av: di %4.3f r(mean)	 	
	qui ivreg2 `outcome' (migrated migUnmarried = t1 c.t1#c.unmarried13) unmarried13 $x i.upazila [pw=wt]  if `samplee' ==1, cluster(UNION)
	qui outreg, merge(dep_final) nod keep(migrated migUnmarried unmarried)   ///
	ctitle("dep.var:", "`labout'" \ "","" )   addrows( "","" \  "Average outcome in control group", `av'  )  ${option} 			
}
foreach outcome in spouse_incLmonth	{
	
	local labout : variable label `outcome'					
	 su `outcome' [aw=wt] if `samplee'==1 & t1==0 
		local av: di %4.0f r(mean)	 	
	 ivreg2  `outcome' (migrated = t1 )  $x  i.upazila [pw=wt] if `samplee' ==1, cluster(UNION)				
	qui outreg, merge(dep_final) nod keep(migrated ) ///
	ctitle("dep.var:", "`labout'" \ "","" )  addrows( "","" \  "Average outcome in control group", `av')  ${option_B} 		

	tempvar samplee
	g `samplee' =  t2==0 & married13 ==1 
	 su `outcome' [aw=wt] if `samplee'==1 & t1==0 
		local av: di %4.0f r(mean)	 		
	 ivreg2  `outcome' (migrated = t1 )  $x  i.upazila [pw=wt] if `samplee' ==1, cluster(UNION)				
	qui outreg, merge(dep_final) nod keep(migrated ) ///
	ctitle("dep.var:", "`labout'" \ "","married bef lottery" )  addrows( "","" \  "Average outcome in control group", `av' )  ${option_B} 		
}
tempvar samplee
g `samplee' =  t2==0 	
foreach outcome in  spouse_inc_zero 	{
	local labout : variable label `outcome'					
	 su `outcome' [aw=wt] if `samplee'==1 & t1==0
		local av: di %4.0f r(mean)	 	
	 ivreg2  `outcome' (migrated = t1 )  $x  i.upazila [pw=wt] if `samplee' ==1, cluster(UNION)				
	qui outreg, merge(dep_final) nod keep(migrated ) ///
	ctitle("dep.var:", "`labout'" \ "","" )  addrows( "","" \  "Average outcome in control group", `av' )  ${option_B} 				
}	


outreg  using "${result}Table7.smcl"  , title (Table 7: Impact of migration on marriage and spouse characteristics using Mobarak et al. (2023)'s data (IV estimates)) replace replay(dep_final) ${option}



